// 招领处理函数模块

const db = require('../db')

const path = require('path')

// 获取招领数据信息
exports.claimList = (req, res) => {
  const sql = 'select * from claim'
  db.query(sql, (err, results) => {
    if (err) {
      return res.ss(err)
    }
    if (results.length <= 0) {
      return res.ss('获取招领数据失败！')
    }
    res.send({
      state: 200,
      message: '获取招领数据成功！',
      data: results
    })
  })
}

// 管理员删除招领信息
exports.dtClaim = (req, res) => {
  const id = req.query.id
  const sql = 'delete from claim where id=?'
  db.query(sql, id, (err, results) => {
    if (err) {
      return res.ss(err)
    }
    if (results.affectedRows !== 1) {
      return res.ss('删除失败！')
    }
    res.ss('删除成功！', 200)
  })
}

// 用户添加招领信息处理函数
exports.addClaim = (req, res) => {
  console.log(req.body)
  const img = 'http://127.0.0.1:8000/upload/image/' + req.file.filename
  const body = {
    ...req.body,
    img: img,
    date: new Date(),
    userid: req.auth.id
  }
  if (!req.file || req.file.fieldname !== 'img') {
    return res.ss('未选择招领图片！')
  }

  const sql = 'insert into claim set ?'
  db.query(sql, body, (err, results) => {
    if (err) {
      return res.ss(err)
    }
    if (results.affectedRows !== 1) {
      return res.ss('添加招领信息失败！')
    }
    res.ss('添加招领信息成功！', 200)
  })
}

// 获取当前用户发布的招领信息
exports.userClaimInfo = (req, res) => {
  const sql = 'select * from claim where userid=?'

  db.query(sql, req.auth.id, (err, results) => {
    if (err) {
      return res.ss(err)
    }
    if (results.length <= 0) {
      return res.ss('当前用户未发布招领信息!')
    }
    res.send({
      state: 200,
      message: '获取用户发布招领信息成功！',
      data: results
    })
  })
}

// 用户删除招领信息处理函数
exports.userClaimdt = (req, res) => {
  const sql = 'delete from claim where id=? and userid=?'
  db.query(sql, [req.query.id, req.auth.id], (err, results) => {
    if (err) {
      return res.ss(err)
    }
    if (results.affectedRows !== 1) {
      return res.ss('删除失物信息失败！')
    }
    res.ss('删除失物信息成功！', 200)
  })
}

// 用户更新招领状态
exports.updateState = (req, res) => {
  const body = req.body
  const sql = 'select * from claim where id=? and userid=?'
  db.query(sql, [body.id, req.auth.id], (err, results1) => {
    if (err) {
      return res.ss(err)
    }
    if (results1.lengt <= 0) {
      return res.ss('获取数据失败')
    }
    const sql = 'update claim set state=? where id=? and userid=?'
    const state = !results1[0].state
    db.query(sql, [state, body.id, req.auth.id], (err, results2) => {
      if (err) {
        return res.ss(err)
      }
      if (results2.affectedRows !== 1) {
        return res.ss('更新状态失败！')
      }
      res.ss('更新状态成功！', 200)
    })
  })
}

// 管理员更新招领状态
exports.adminUpdateState = (req, res) => {
  const body = req.body
  const sql = 'select * from claim where id=?'
  db.query(sql, body.id,  (err, results1) => {
    if (err) {
      return res.ss(err)
    }
    if (results1.lengt <= 0) {
      return res.ss('获取数据失败！')
    }
    const sql = 'update claim set state=? where id=?'
    const state = !results1[0].state
    console.log(state)
    db.query(sql, [state, body.id], (err, results2) => {
      if (err) {
        return res.ss(err)
      }
      if (results2.affectedRows !== 1) {
        return res.ss('更改状态失败！')
      }
      res.ss('更改状态成功！', 200)
    })
  })
}